/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SessionBeans;

import SessionBeans.entites.FolderService;
import entities.AccountHeader;
import entities.Folder;
import entities.User;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.*;
import security.Md5;

/**
 *
 * @author Tomas
 */
@Stateless
@LocalBean
public class UserSessionBean {

    @EJB
    private FolderService folderService;
    @PersistenceContext(unitName = "tCloudEE-ejbPU")
    private EntityManager em;

    public boolean tryLogin(String userName, String password) {
        TypedQuery<User> createQuery = em.createQuery("Select OBJECT(u) from User u where u.name LIKE '" + userName.toLowerCase() + "' and u.password LIKE '" + password.toLowerCase() + "'", User.class);
        try {
            User singleResult = createQuery.getSingleResult();
            return singleResult != null;
        } catch (NoResultException ex) {
            return false;
        }

    }

    public User login(String userName, String password) {
        TypedQuery<User> createQuery = em.createQuery("Select OBJECT(u) from User u where u.name LIKE '" + userName.toLowerCase() + "' and u.password LIKE '" + password.toLowerCase() + "'", User.class);
        User singleResult = createQuery.getSingleResult();
        return singleResult;
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    public void createUser(String userName, String password, String email) {
        password = Md5.MD5(password.toLowerCase());
        User user = new User(null, userName.toLowerCase(), email, new Date());
        user.setPassword(password);
        AccountHeader acc = new AccountHeader(null, userName + "Acc", new Date(), 0);



        //folder = em.merge(folder);        

        user = em.merge(user);
        Folder folder = folderService.newFolder(user.getId(), userName + "Root");
        acc.setOwnerUser(user);
        acc.setRootFolder(folder);
        em.merge(acc);
    }

    public void updateUser(User user) {
        User find = em.find(User.class, user.getId());
        find.setPassword(user.getPassword());
        find.setEmail(user.getEmail());
        em.persist(find);
    }

    public boolean checkUserName(String userName) {
        TypedQuery<User> createQuery = em.createQuery("Select OBJECT(u) from User u where u.name LIKE '" + userName.toLowerCase() + "'", User.class);
        try {
            User singleResult = createQuery.getSingleResult();
            return singleResult != null;
        } catch (NoResultException ex) {
            return false;
        }
    }

    public boolean checkEmail(String email) {
        TypedQuery<User> createQuery = em.createQuery("Select OBJECT(u) from User u where u.email LIKE '" + email.toLowerCase() + "'", User.class);
        try {
            User singleResult = createQuery.getSingleResult();
            return singleResult != null;
        } catch (NoResultException ex) {
            return false;
        }
    }

    public User findUser(String userName) {
        TypedQuery<User> createQuery = em.createQuery("Select OBJECT(u) from User u where u.name LIKE '" + userName.toLowerCase() + "'", User.class);

        User singleResult = createQuery.getSingleResult();
        return singleResult;
    }
    
    public List<User> findUsers(String query){
        Query createNativeQuery = em.createNativeQuery("select * from appuser where name like '" + query + "%'", User.class);
        List<User> resultList = createNativeQuery.getResultList();
        
        return resultList;
    }
}
